package com.ourteam.product.requirement.dao.impl;

import com.ourteam.product.requirement.dao.IProductRequirementRelateDAO;
import com.ourteam.product.requirement.dao.ProductRequirementRelate;
import com.ourteam.product.requirement.dao.ProductRequirementRelateDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-05-03 16:45:42
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductRequirementRelateDAOJdbcImpl
    extends AbstractSpringDAO implements IProductRequirementRelateDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, RequirementId, RelatedRequirementId, RelatedType, Status,
            Remarks, RequestCode, RequestName, RequestContent, RequestLevel,
            RequestStatus, RequestType, RelatedRequestCode, RelatedRequestName,
            RelatedRequestContent, RelatedRequestLevel, RelatedRequestStatus,
            RelatedRequestType
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.RequirementId,
            _DataObjectProperty.RelatedRequirementId,
            _DataObjectProperty.RelatedType, _DataObjectProperty.Status,
            _DataObjectProperty.Remarks, _DataObjectProperty.RequestCode,
            _DataObjectProperty.RequestName, _DataObjectProperty.RequestContent,
            _DataObjectProperty.RequestLevel, _DataObjectProperty.RequestStatus,
            _DataObjectProperty.RequestType,
            _DataObjectProperty.RelatedRequestCode,
            _DataObjectProperty.RelatedRequestName,
            _DataObjectProperty.RelatedRequestContent,
            _DataObjectProperty.RelatedRequestLevel,
            _DataObjectProperty.RelatedRequestStatus,
            _DataObjectProperty.RelatedRequestType
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductRequirementRelate, ProductRequirementRelateDAOQueryBean> daoHelper;

    public AbstractProductRequirementRelateDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductRequirementRelate, ProductRequirementRelateDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.product.requirement.dao.IProductRequirementDAO.EntityName)
                                .append(" PRODUCT_REQUIREMENT ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.product.requirement.dao.IProductRequirementDAO.Id,
                    com.ourteam.product.requirement.dao.IProductRequirementDAO.EntityName +
                    ".", "PRODUCT_REQUIREMENT."));
            relatedJoinSQLBuffer.append(" = ").append(RequirementId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.product.requirement.dao.IProductRequirementDAO.EntityName)
                                .append(" RELATED_PRODUCT_REQUIREMENT ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.product.requirement.dao.IProductRequirementDAO.Id,
                    com.ourteam.product.requirement.dao.IProductRequirementDAO.EntityName +
                    ".", "RELATED_PRODUCT_REQUIREMENT."));
            relatedJoinSQLBuffer.append(" = ").append(RelatedRequirementId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductRequirementRelate
         * @throws Exception
         */
    public boolean checkIsUnique(
        ProductRequirementRelate aProductRequirementRelate)
        throws Exception {
        if (aProductRequirementRelate == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductRequirementRelate.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductRequirementRelate
     * @throws Exception
     */
    public void insert(ProductRequirementRelate aProductRequirementRelate)
        throws Exception {
        super.doInsert(aProductRequirementRelate);
    }

    /**
         * Batch Insert Datas
         * @param aProductRequirementRelate
         * @throws Exception
         */
    public void batchInsert(
        ProductRequirementRelate[] aProductRequirementRelates)
        throws Exception {
        super.doBatchInsert(aProductRequirementRelates);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(ProductRequirementRelateDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(ProductRequirementRelate aProductRequirementRelate)
        throws Exception {
        delete(aProductRequirementRelate.getId());
    }

    /**
     * Update Data Selective
     * @param aProductRequirementRelate
     * @throws Exception
     */
    public void update(ProductRequirementRelate aProductRequirementRelate)
        throws Exception {
        if (aProductRequirementRelate.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductRequirementRelate);

        ProductRequirementRelate tempObj = findById(aProductRequirementRelate.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductRequirementRelate, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductRequirementRelates[]
     * @throws Exception
     */
    public void batchUpdate(
        ProductRequirementRelate[] aProductRequirementRelates)
        throws Exception {
        super.doBatchUpdate(aProductRequirementRelates);
    }

    /**
         * Update Data By Condictions
         * @param aProductRequirementRelate
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(ProductRequirementRelate aProductRequirementRelate,
        ProductRequirementRelateDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aProductRequirementRelate, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(ProductRequirementRelateDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(
        ProductRequirementRelate aProductRequirementRelate)
        throws Exception {
        if (aProductRequirementRelate.getRelatedType() == null) {
            throw new Exception(
                "The ProductRequirementRelate.relatedType value is not allow NULL");
        }

        if (aProductRequirementRelate.getStatus() == null) {
            throw new Exception(
                "The ProductRequirementRelate.status value is not allow NULL");
        }

        if (aProductRequirementRelate.getRemarks() == null) {
            throw new Exception(
                "The ProductRequirementRelate.remarks value is not allow NULL");
        }

        if (aProductRequirementRelate.getRequestCode() == null) {
            throw new Exception(
                "The ProductRequirementRelate.requestCode value is not allow NULL");
        }

        if (aProductRequirementRelate.getRequestName() == null) {
            throw new Exception(
                "The ProductRequirementRelate.requestName value is not allow NULL");
        }

        if (aProductRequirementRelate.getRequestContent() == null) {
            throw new Exception(
                "The ProductRequirementRelate.requestContent value is not allow NULL");
        }

        if (aProductRequirementRelate.getRequestLevel() == null) {
            throw new Exception(
                "The ProductRequirementRelate.requestLevel value is not allow NULL");
        }

        if (aProductRequirementRelate.getRelatedRequestCode() == null) {
            throw new Exception(
                "The ProductRequirementRelate.relatedRequestCode value is not allow NULL");
        }

        if (aProductRequirementRelate.getRelatedRequestName() == null) {
            throw new Exception(
                "The ProductRequirementRelate.relatedRequestName value is not allow NULL");
        }

        if (aProductRequirementRelate.getRelatedRequestContent() == null) {
            throw new Exception(
                "The ProductRequirementRelate.relatedRequestContent value is not allow NULL");
        }

        if (aProductRequirementRelate.getRelatedRequestLevel() == null) {
            throw new Exception(
                "The ProductRequirementRelate.relatedRequestLevel value is not allow NULL");
        }

        if (aProductRequirementRelate.getRelatedRequestStatus() == null) {
            throw new Exception(
                "The ProductRequirementRelate.relatedRequestStatus value is not allow NULL");
        }

        if (aProductRequirementRelate.getRequestStatus() == null) {
            throw new Exception(
                "The ProductRequirementRelate.requestStatus value is not allow NULL");
        }

        if (aProductRequirementRelate.getRequestType() == null) {
            throw new Exception(
                "The ProductRequirementRelate.requestType value is not allow NULL");
        }

        if (aProductRequirementRelate.getRelatedRequestType() == null) {
            throw new Exception(
                "The ProductRequirementRelate.relatedRequestType value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductRequirementRelate findById(final long id)
        throws Exception {
        ProductRequirementRelateDAOQueryBean queryBean = new ProductRequirementRelateDAOQueryBean();

        ProductRequirementRelateDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductRequirementRelate[] ProductRequirementRelates = query(queryBean);

        if (ProductRequirementRelates.length == 0) {
            return null;
        } else {
            return ProductRequirementRelates[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductRequirementRelate[]
         * @throws Exception
         */
    public ProductRequirementRelate[] query(
        ProductRequirementRelateDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcProductRequirementRelateRowMapper());

        return (ProductRequirementRelate[]) result.toArray(new ProductRequirementRelate[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductRequirementRelateDAOQueryBean queryBean)
        throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(ProductRequirementRelateDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(
        ProductRequirementRelate[] aProductRequirementRelates,
        ProductRequirementRelateDAOQueryBean queryBean)
        throws Exception {
        daoHelper.synchronize(aProductRequirementRelates, queryBean);
    }

    public void synchronize(
        ProductRequirementRelate[] aProductRequirementRelates,
        ProductRequirementRelateDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductRequirementRelate> callBack)
        throws Exception {
        daoHelper.synchronize(aProductRequirementRelates, queryBean, callBack);
    }

    public static class JdbcProductRequirementRelateRowMapper
        implements RowMapper<ProductRequirementRelate> {
        public ProductRequirementRelate mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            ProductRequirementRelate object = new ProductRequirementRelate();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object requirementId = null;

                requirementId = resultSet.getObject(columnIndex++);

                object.setRequirementIdNull(resultSet.getBoolean(columnIndex++));

                if (requirementId != null) {
                    setBeanValue(object, _DataObjectProperty.RequirementId,
                        requirementId);
                }

                Object relatedRequirementId = null;

                relatedRequirementId = resultSet.getObject(columnIndex++);

                object.setRelatedRequirementIdNull(resultSet.getBoolean(
                        columnIndex++));

                if (relatedRequirementId != null) {
                    setBeanValue(object,
                        _DataObjectProperty.RelatedRequirementId,
                        relatedRequirementId);
                }

                Object relatedType = null;

                relatedType = resultSet.getObject(columnIndex++);

                object.setRelatedTypeNull(resultSet.getBoolean(columnIndex++));

                if (relatedType != null) {
                    setBeanValue(object, _DataObjectProperty.RelatedType,
                        relatedType);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object requestCode = null;

                requestCode = resultSet.getObject(columnIndex++);

                object.setRequestCodeNull(resultSet.getBoolean(columnIndex++));

                if (requestCode != null) {
                    setBeanValue(object, _DataObjectProperty.RequestCode,
                        requestCode);
                }

                Object requestName = null;

                requestName = resultSet.getObject(columnIndex++);

                object.setRequestNameNull(resultSet.getBoolean(columnIndex++));

                if (requestName != null) {
                    setBeanValue(object, _DataObjectProperty.RequestName,
                        requestName);
                }

                Object requestContent = null;

                requestContent = resultSet.getObject(columnIndex++);

                object.setRequestContentNull(resultSet.getBoolean(columnIndex++));

                if (requestContent != null) {
                    setBeanValue(object, _DataObjectProperty.RequestContent,
                        requestContent);
                }

                Object requestLevel = null;

                requestLevel = resultSet.getObject(columnIndex++);

                object.setRequestLevelNull(resultSet.getBoolean(columnIndex++));

                if (requestLevel != null) {
                    setBeanValue(object, _DataObjectProperty.RequestLevel,
                        requestLevel);
                }

                Object requestStatus = null;

                requestStatus = resultSet.getObject(columnIndex++);

                object.setRequestStatusNull(resultSet.getBoolean(columnIndex++));

                if (requestStatus != null) {
                    setBeanValue(object, _DataObjectProperty.RequestStatus,
                        requestStatus);
                }

                Object requestType = null;

                requestType = resultSet.getObject(columnIndex++);

                object.setRequestTypeNull(resultSet.getBoolean(columnIndex++));

                if (requestType != null) {
                    setBeanValue(object, _DataObjectProperty.RequestType,
                        requestType);
                }

                Object relatedRequestCode = null;

                relatedRequestCode = resultSet.getObject(columnIndex++);

                object.setRelatedRequestCodeNull(resultSet.getBoolean(
                        columnIndex++));

                if (relatedRequestCode != null) {
                    setBeanValue(object,
                        _DataObjectProperty.RelatedRequestCode,
                        relatedRequestCode);
                }

                Object relatedRequestName = null;

                relatedRequestName = resultSet.getObject(columnIndex++);

                object.setRelatedRequestNameNull(resultSet.getBoolean(
                        columnIndex++));

                if (relatedRequestName != null) {
                    setBeanValue(object,
                        _DataObjectProperty.RelatedRequestName,
                        relatedRequestName);
                }

                Object relatedRequestContent = null;

                relatedRequestContent = resultSet.getObject(columnIndex++);

                object.setRelatedRequestContentNull(resultSet.getBoolean(
                        columnIndex++));

                if (relatedRequestContent != null) {
                    setBeanValue(object,
                        _DataObjectProperty.RelatedRequestContent,
                        relatedRequestContent);
                }

                Object relatedRequestLevel = null;

                relatedRequestLevel = resultSet.getObject(columnIndex++);

                object.setRelatedRequestLevelNull(resultSet.getBoolean(
                        columnIndex++));

                if (relatedRequestLevel != null) {
                    setBeanValue(object,
                        _DataObjectProperty.RelatedRequestLevel,
                        relatedRequestLevel);
                }

                Object relatedRequestStatus = null;

                relatedRequestStatus = resultSet.getObject(columnIndex++);

                object.setRelatedRequestStatusNull(resultSet.getBoolean(
                        columnIndex++));

                if (relatedRequestStatus != null) {
                    setBeanValue(object,
                        _DataObjectProperty.RelatedRequestStatus,
                        relatedRequestStatus);
                }

                Object relatedRequestType = null;

                relatedRequestType = resultSet.getObject(columnIndex++);

                object.setRelatedRequestTypeNull(resultSet.getBoolean(
                        columnIndex++));

                if (relatedRequestType != null) {
                    setBeanValue(object,
                        _DataObjectProperty.RelatedRequestType,
                        relatedRequestType);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
